package com.iyouqu.dycm.search.utils;

import java.util.List;

import com.aliyun.opensearch.OpenSearchClient;
import com.aliyun.opensearch.SearcherClient;
import com.aliyun.opensearch.sdk.dependencies.com.google.common.collect.Lists;
import com.aliyun.opensearch.sdk.dependencies.org.json.JSONArray;
import com.aliyun.opensearch.sdk.dependencies.org.json.JSONObject;
import com.aliyun.opensearch.sdk.generated.OpenSearch;
import com.aliyun.opensearch.sdk.generated.search.Config;
import com.aliyun.opensearch.sdk.generated.search.Order;
import com.aliyun.opensearch.sdk.generated.search.SearchFormat;
import com.aliyun.opensearch.sdk.generated.search.SearchParams;
import com.aliyun.opensearch.sdk.generated.search.Sort;
import com.aliyun.opensearch.sdk.generated.search.SortField;
import com.aliyun.opensearch.sdk.generated.search.general.SearchResult;
import com.iyouqu.dycm.search.bean.Search;

public class OpenSearchMgrOnline {
	private static String appName = PropUtil.getProperty("search.opensearch.appName", "");
	private static String accesskey = PropUtil.getProperty("search.opensearch.accesskey", "");
	private static String secret = PropUtil.getProperty("search.opensearch.secret", "");
	private static String host = PropUtil.getProperty("search.opensearch.host",
			"http://opensearch-cn-hangzhou.aliyuncs.com");

	private static OpenSearch openSearch;
	private static OpenSearchClient serviceClient;
	private static SearcherClient searcherClient;
	private static Config config;

	static {
		openSearch = new OpenSearch(accesskey, secret, host);
		serviceClient = new OpenSearchClient(openSearch);
		searcherClient = new SearcherClient(serviceClient);

		config = new Config(Lists.newArrayList(appName));
		config.setSearchFormat(SearchFormat.JSON);
		config.setFetchFields(Lists.newArrayList("ad_id", "ad_type", "start_time", "end_time", "city"));
	}

	public static List<Search> search(int start, int pageSize) {
		try {
			config.setStart(start);
			config.setHits(pageSize);
			SearchParams searchParams = new SearchParams(config);

			// 设置sort条件
			Sort sorter = new Sort();
			sorter.addToSortFields(new SortField("ad_id", Order.INCREASE));
			searchParams.setSort(sorter);

			// 解析结果
			SearchResult searchResult = searcherClient.execute(searchParams);
			String result = searchResult.getResult();
			JSONObject obj = new JSONObject(result);
			JSONArray jary = obj.getJSONObject("result").getJSONArray("items");
			if (jary != null && jary.length() > 0) {
				List<Search> list = Lists.newArrayList();

				for (int i = 0; i < jary.length(); i++) {
					Search ss = new Search();
					ss.setAd_id(jary.getJSONObject(i).getString("ad_id"));
					ss.setAd_type(jary.getJSONObject(i).get("ad_type").toString());
					ss.setStart_time(jary.getJSONObject(i).getLong("start_time"));
					ss.setEnd_time(jary.getJSONObject(i).getLong("end_time"));
					ss.setCity(jary.getJSONObject(i).getString("city").split(","));
					list.add(ss);
				}

				return list;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
